package com.baibei.order.bill.sticky_header;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.baibei.order.bill.viewtype.ViewType;

/**
 * 滚动置顶带日期
 * Created by shenminjie on 2017/8/9.
 * mail:shenminjie@100bei.com
 */

public class StickyHeaderScrollListener extends RecyclerView.OnScrollListener {

    /**
     * 上层传递，统一处理试图
     */
    private TextView mTvDate;

    public StickyHeaderScrollListener(TextView textView) {
        this.mTvDate = textView;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        //第一个视图
        View stickyInfoView = recyclerView.findChildViewUnder(0, 0);
        if (stickyInfoView.getContentDescription() != null) {
            mTvDate.setText(stickyInfoView.getContentDescription());
        }
        View secondItemView = recyclerView.findChildViewUnder(0, mTvDate.getMeasuredHeight());
        if (secondItemView != null && secondItemView.getTag() != null) {
            String viewType = (String) secondItemView.getTag();
            if (ViewType.DATE == ViewType.valueOf(viewType)) {
                //移动的高度------主要移动置顶的view
                int offsetY = secondItemView.getTop() - mTvDate.getMeasuredHeight();
                if (secondItemView.getTop() > 0) {
                    mTvDate.setTranslationY(offsetY);
                } else {
                    mTvDate.setTranslationY(0);
                }
            } else {
                //还原
                mTvDate.setTranslationY(0);
            }
        }
    }
}
